package com.admin.controller;

import com.admin.entity.Category;
import com.admin.service.CategoryService;
import com.admin.entity.Delivery;
import com.admin.service.DeliveryService;
import com.admin.service.StockService;
import com.admin.entity.Stock;
import com.admin.entity.Storage;
import com.admin.service.StorageService;
import com.admin.entity.Supplier;
import com.admin.service.SupplierService;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.base.entity.DateUtils;
import com.admin.sys.utils.excel.ExportExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
*【库存信息】页面接口
*/
@RestController
@RequestMapping("/admin/stock")
public class StockController extends BaseController {

    @Autowired
    private StockService stockService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CategoryService categoryService;

    /**
    * 【库存信息】根据条件分页查询
    * @param page
    * @param stock
    * @return
    */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Stock> page, Stock stock) {
        IPage<Stock> iPage = stockService.getPage(page, stock);
        List<Stock> records = iPage.getRecords();
        for (Stock record : records) {
            //出库数量判断
            Storage storage = new Storage();
            storage.setSupplierId(record.getSupplierId());
            storage.setGoodsId(record.getId());
            List<Storage> storageList = storageService.getList(storage);
            int storageSum = storageList.stream().mapToInt(Storage::getNumber).sum();
            Delivery params = new Delivery();
            params.setSupplierId(record.getSupplierId());
            params.setGoodsId(record.getId());
            List<Delivery> deliveryList = deliveryService.getList(params);
            int deliverySum = deliveryList.stream().mapToInt(Delivery::getNumber).sum();
            //剩余数量 = 入库数量-出库数量
            int surplusNum = storageSum - deliverySum;
            record.setSurplusNum(surplusNum);
        }
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【库存信息】根据条件查询
    * @param stock
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Stock stock) {
        List<Stock> list = stockService.getList(stock);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    * 【库存信息】根据对象查询
    * @param stock
    * @return
    */
    @RequestMapping("/get")
    public ResultInfo get(Stock stock) {
        Stock entity = stockService.get(stock);
        return ResultInfo.ok("获取对象成功", entity);
    }

    /**
    * 【库存信息】提交(新增或修改)
    * @param stock
    * @return
    */
    @RequestMapping("/sub")
    public ResultInfo insert(Stock stock) {
        if (StringUtils.isEmpty(stock.getId())) { //新增
            stockService.insert(stock);
        } else {//修改
            stockService.update(stock);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【库存信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    public ResultInfo delete(String id) {
        stockService.delete(id);
        return ResultInfo.ok("删除成功!");
    }

    /**
    * 【库存信息】批量删除
    * @param ids
    * @return
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        stockService.delAll(ids);
        return ResultInfo.ok("批量删除成功！");
    }


    /**
    *【库存信息】导出
    */
    @RequestMapping(value = "/export")
    public void exportFile(Stock stock, HttpServletResponse response) {
        try {
            String fileName = "库存信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Stock> list = stockService.getList(stock);
            for (Stock entity : list) {
                //出库数量判断
                Storage storage = new Storage();
                storage.setSupplierId(entity.getSupplierId());
                storage.setGoodsId(entity.getId());
                List<Storage> storageList = storageService.getList(storage);
                int storageSum = storageList.stream().mapToInt(Storage::getNumber).sum();
                Delivery params = new Delivery();
                params.setSupplierId(entity.getSupplierId());
                params.setGoodsId(entity.getId());
                List<Delivery> deliveryList = deliveryService.getList(params);
                int deliverySum = deliveryList.stream().mapToInt(Delivery::getNumber).sum();
                //剩余数量 = 入库数量-出库数量
                int surplusNum = storageSum - deliverySum;
                entity.setSurplusNum(surplusNum);
                //查询名称
                Supplier supplier = supplierService.get(entity.getSupplierId());
                entity.setSupplierId(supplier.getName());
                Category category = categoryService.get(entity.getCategoryId());
                entity.setCategoryId(category.getName());
            }
            new ExportExcel("库存信息", Stock.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}



